<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-8-19
 * Func   彩票接口
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('lottery_period');
$ltable = Table('lottery_period_lost');
$ltable2 = Table('lottery_period_lost2');
$lptable = Table('lottery_play_tid');
$lptable2 = Table('lottery_play');

$id = intval($_GET['id']);
!$id && $id = intval($_GET['gameid']);
!$id && $id = intval($_GET['gameID']);

$data = $lottery[$id];
(!in_array($a,['rulecredit','play3','waybill','forget']) && !$data) && exit('');

if($a == 'nowperiod')
{
	//	上一期
	$lottery0 = $db->getrow('SELECT * FROM '.$table.' WHERE cid = '.$data['id'].' AND rednum != \'\' ORDER BY stoptime DESC');
	
	//	当前售卖期
	$lottery1 = $db->getrow('SELECT * FROM '.$table.' WHERE cid = '.$data['id'].' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime');
	
	//	下一期
	$lottery2 = $db->getrow('SELECT * FROM '.$table.' WHERE cid = '.$data['id'].' AND stoptime >= '.TIMESTAMP.' ORDER BY stoptime LIMIT 1,1');
	
	//	上一期、当前期、下一期
	$nowperiod = [];
	$nowperiod['fpreviousperiod']	=	$lottery0['period'];
	$nowperiod['fpreviousresult']	=	$lottery0['rednum'];
	$nowperiod['fid']				=	$lottery1['id'];
	$nowperiod['fnumberofperiod']	=	$lottery1['period'];
	$nowperiod['fstarttime']		=	date('Y/m/d H:i:s',$lottery1['starttime']);
	$nowperiod['flottostarttime']	=	date('Y/m/d H:i:s',$lottery1['stoptime']);
	$nowperiod['fstatus']			=	1;//$data['sales'] ? ($lottery1['stoptime'] > TIMESTAMP ? 1 : 0) : 0;
	$nowperiod['fclosetime']		=	date('Y/m/d H:i:s',$lottery1['stoptime']);
	$nowperiod['fnextperiod']		=	$lottery2['period'];
	$nowperiod['fnextstarttime']	=	date('Y/m/d H:i:s',$lottery2['starttime']);
	$nowperiod['fsettlefid']		=	$lottery2['id'];
	$nowperiod['fsettlenumber']		=	$lottery0['period'];
	$nowperiod['fsettletime']		=	date('Y/m/d H:i:s',$lottery0['stoptime']);
	$nowperiod['ServerTime']		=	date('Y/m/d H:i:s');
	$nowperiod['fisstopseles']		=	$data['sales'] ? ($lottery1['stoptime'] > TIMESTAMP ? 'False' : 'True') : 'True';
	
	//	香港时时彩
	if($data['id'] == 23)
	{
		$nowperiod['fspecialcodeclosetime']	=	date('Y/m/d H:i:s',$lottery1['stoptime']);
		$nowperiod['fcodeclosetime']		=	date('Y/m/d H:i:s',$lottery1['stoptime']);
		$nowperiod['fcodestatus']			=	$nowperiod['fstatus'] ? 'true' : 'false';
		$nowperiod['fevencodeclosetime']	=	date('Y/m/d H:i:s',$lottery1['stoptime']);
		$nowperiod['fevencodestatus']		=	$nowperiod['fstatus'] ? 'true' : 'false';
		$nowperiod['fbothsidecodeclosetime']=	date('Y/m/d H:i:s',$lottery1['stoptime']);
		$nowperiod['fbothsidecodestatus']	=	$nowperiod['fstatus'] ? 'true' : 'false';
	}
	
	//	信用玩法
	if($data['credit'])
	{
		$nowperiod['fstatus'] = strval($nowperiod['fstatus']);
	}

	echo json_encode($nowperiod);
	exit;
}
else if($a == 'play')	//	玩法
{
	$json = '';
	if($data)
	{
		//	ssc:enableMapping
		//	kl8:mapping
		//	3D:enableMapping
		//	K3:mapping
		//	PK10:
		//	11X5:mapping
		
		$res = $db->getall('SELECT * FROM '.$lptable.' WHERE ptid = \''.$data['ptid'].'\' AND status = 1');
		foreach($res as $val)
		{
			$json .= ','.$val['id'].':{';
			$json .= 'description:"'.$val['intro'].'",';
			$json .= 'example:"'.$val['example'].'",';
			$json .= 'rule:"'.$val['rule'].'",';
			$json .= 'tpl:"'.$val['tpl'].'",';
			$val['samepos'] && $json .= 'isAllowSamePos:!0,';
			$val['groupinput'] && $json .= 'isGroupInput:!0,';
			
			$val['algorithm'] && $json .= 'algorithm:'.$val['algorithm'].',';
			$val['sub'] && $json .= 'sub:'.$val['sub'].',';
			$val['format'] && $json .= 'format:'.$val['format'].',';
			$val['rank'] && $json .= 'rank:'.$val['rank'].',';
			
			$val['vm'] && $json .= 'vm:"'.$val['vm'].'",';
			$val['count'] && $json .= 'count:'.$val['count'].',';
			
			$val['mapping'] && $json .= in_array($val['id'],[1774,1451]) ? 'mapping:'.$val['mapping'].',' : 'enableMapping:'.$val['mapping'].',mapping:'.$val['mapping'].',';
			
			$val['checkpos'] && $json .= 'defaultCheckedPos:'.$val['checkpos'].',';
			$val['poslen'] && $json .= 'posLen:'.$val['poslen'].',';
			$val['checklen'] && $json .= 'maxCheckedLen:'.$val['checklen'].',';
			$val['uselv2'] && $json .= 'isUseLv2Name:'.$val['uselv2'].',';
			
			$val['id'] == 3147 && $json .= 'isRadio:!0,';
			
			$json .= 'name:"'.$val['name'].'"';
			$json .= '}';
		}
	}
	
	$lens = ['SSC'=>1,'11X5'=>2,'3D'=>1,'K3'=>1,'KL8'=>2,'PK10'=>2];	//	彩票号码个数
	$numberLen = intval($lens[$data['ptid']]);
	
	$lens = ['SSC'=>3,'11X5'=>6,'3D'=>0,'K3'=>0,'KL8'=>0,'PK10'=>2];	//	彩票号码个数
	$defaultNavIndex = intval($lens[$data['ptid']]);
	
	$res = '!function(e){function n(r){if(m[r])return m[r].exports;var a=m[r]={exports:{},id:r,loaded:!1};return e[r].call(a.exports,a,a.exports,n),a.loaded=!0,a.exports}var m={};return n.m=e,n.c=m,n.p="",n(0)}({0:function(e,n,m){e.exports=m(82)},82:function(e,n){window.CONFIG=function(){';
	$res.= 'return{numberLen:'.$numberLen.',defaultNavIndex:'.$defaultNavIndex.',inputTip:"程序会自动过滤掉不合法的号码。您也可以点击“<b>删除重复号</b>”按钮对输入内容进行格式化。"'.$json.'}}()}});';
	
	echo str_replace("\n",'',$res);
	exit;
}
else if($a == 'play2')	//	玩法2
{
	/*$tmp = $db->getrow('SELECT * FROM '.Table('lottery_play_json').' WHERE cid = '.$data['id']);
	
	echo 'var navListJson = \''.$tmp['content'].'\';';
	exit;*/
	$json = [];
	
	if($data['credit'])	//	信用玩法
	{
		$res1 = $db->getall('SELECT * FROM '.$lptable2.' WHERE cid = '.$data['id'].' AND pid = 0 AND status = 1 ORDER BY playid');
		foreach($res1 as $key1 => $val1)
		{
			$json[$key1] = [];
			$json[$key1]['Id']	=	intval($val1['playid']);
			$json[$key1]['N']	=	$val1['name'];
			$json[$key1]['PIs']	=	[];
			$json[$key1]['C']	=	[];
			
			$res2 = $db->getall('SELECT * FROM '.$lptable2.' WHERE cid = '.$data['id'].' AND pid = '.$val1['id'].' AND status = 1 ORDER BY playid');
			foreach($res2 as $key2 => $val2)
			{
				$son2 = [];
				$son2['Sord']	=	intval($val2['sord']);
				$son2['Id']		=	intval($val2['playid']);
				$son2['P']		=	intval($val2['parentid']);
				$son2['N']		=	$val2['name'];
				$son2['C']		=	[];
				
				$res3 = $db->getall('SELECT * FROM '.$lptable2.' WHERE cid = '.$data['id'].' AND pid = '.$val2['id'].' AND status = 1 ORDER BY playid');
				foreach($res3 as $key3 => $val3)
				{
					$son3 = [];
					$son3['I']	=	intval($val3['playid']);
					$son3['N']	=	$val3['name'];
					$son3['MB']	=	$val3['mb'];
					$son3['MxO']=	$val3['maxprize'];
					$son3['MiO']=	$val3['minprize'];
					$son3['SI']	=	intval($val3['si']);
					$son3['St']	=	$val3['st'] ? true : false;
					$val3['l'] && $son3['L'] = intval($val3['l']);
					
					$son2['C'][] = $son3;
				}
				
				$json[$key1]['PIs'][] = intval($val2['playid']);
				$json[$key1]['C'][] = $son2;
			}
		}		
	}
	else
	{
		$res1 = $db->getall('SELECT * FROM '.$lptable2.' WHERE cid = '.$data['id'].' AND pid = 0 AND status = 1');
		foreach($res1 as $key1 => $val1)
		{
			$json[$key1] = [];
			$json[$key1]['Id']	=	intval($val1['playid']);
			$json[$key1]['P']	=	0;
			$json[$key1]['N']	=	$val1['name'];
			$json[$key1]['c']	=	[];
			
			$son1 = [];		
			$res2 = $db->getall('SELECT * FROM '.$lptable2.' WHERE cid = '.$data['id'].' AND pid = '.$val1['id'].' AND status = 1');
			foreach($res2 as $key2 => $val2)
			{
				!is_array($son1[$val2['gname']]) && $son1[$val2['gname']] = ['N'=>$val2['gname'],'C'=>[]];
				
				$son2 = [];
				$son2['I']	=	intval($val2['playid']);
				$son2['P']	=	intval($val2['parentid']);
				$son2['N']	=	$val2['name'];
				$son2['TP']	=	intval($val2['tp']);
				$son2['C']	=	[];
				
				$res3 = $db->getall('SELECT * FROM '.$lptable2.' WHERE cid = '.$data['id'].' AND pid = '.$val2['id'].' AND status = 1');
				foreach($res3 as $key3 => $val3)
				{
					$son2['C'][$key3] = [];
					$son2['C'][$key3]['I']	=	intval($val3['playid']);
					$son2['C'][$key3]['N']	=	$val3['name'];
					$son2['C'][$key3]['MB']	=	$val3['mb'];
					$son2['C'][$key3]['MxO']=	$val3['maxprize'];
					$son2['C'][$key3]['MiO']=	$val3['minprize'];
					$son2['C'][$key3]['SI']	=	intval($val3['si']);
				}
				
				$son1[$val2['gname']]['C'][] = $son2;
			}
			
			foreach($son1 as $sval)
			{
				$json[$key1]['c'][] = $sval;
			}
		}
	}
	
	echo 'var navListJson = \''.str_replace("\n",'',json_encode($json,JSON_UNESCAPED_UNICODE)).'\';';
	exit;
}
else if($a == 'play3')	//	玩法3
{
	echo '[{"GamePlayID":186,"GameName":"正码二单双","ItemName":"单","Count":2},{"GamePlayID":188,"GameName":"正码二尾大小","ItemName":"尾大","Count":2},{"GamePlayID":197,"GameName":"正码三尾大小","ItemName":"尾大","Count":4},{"GamePlayID":203,"GameName":"正码四大小","ItemName":"大","Count":2},{"GamePlayID":205,"GameName":"正码四四喜","ItemName":"寿","Count":2},{"GamePlayID":207,"GameName":"特码球号","ItemName":"6","Count":2},{"GamePlayID":213,"GameName":"特码单双","ItemName":"特双","Count":2},{"GamePlayID":212,"GameName":"特码大小","ItemName":"特小","Count":5},{"GamePlayID":215,"GameName":"特码尾大小","ItemName":"尾大","Count":5},{"GamePlayID":214,"GameName":"特码四喜","ItemName":"禄","Count":5},{"GamePlayID":312,"GameName":"正二VS特码","ItemName":"龙","Count":5},{"GamePlayID":315,"GameName":"正四VS特码","ItemName":"龙","Count":2},{"GamePlayID":578,"GameName":"正码三合数单双","ItemName":"合单","Count":2},{"GamePlayID":580,"GameName":"特码合数单双","ItemName":"合双","Count":2},{"GamePlayID":587,"GameName":"总和单双","ItemName":"总单","Count":8},{"GamePlayID":601,"GameName":"正码四色波","ItemName":"绿波","Count":2},{"GamePlayID":602,"GameName":"特码色波","ItemName":"绿波","Count":2},{"GamePlayID":630,"GameName":"上下","ItemName":"和","Count":2},{"GamePlayID":752,"GameName":"全5中1","ItemName":"6","Count":6}]';
	exit;
}
else if($a == 'playlist')	//	玩法3
{
	$tmp = $db->getrow('SELECT * FROM '.Table('lottery_play_json').' WHERE cid = '.$data['id']);
	$tmp = json_decode($tmp['content'],true);
	
	$json = [];
	foreach($tmp as $val)
	{
		$json[] = ['ID'=>$val['Id'],'Name'=>$val['N']];
	}
	
	echo json_encode($json,JSON_UNESCAPED_UNICODE);
	exit;
}
else if($a == 'waybill')	//	暂不清楚
{
	echo '["2","7","6","1","7","9","3","6","4","4","5","2","1","6","0","0","0","9","3","4","3","7","2","4","0","4","5","2","3","0","5","2","9","5","4","0","8","9","2","0","5","0","6","6","1","9","2","6","2","2","7","7","5","2","5","2","1","9","6","5","4","7","0","3","5","2","0","2","0","5","1","3","0","5","6","4","0","5","4","4","9","6","2","3","6","2","0","7","5","3","8","6","6","8","2","5","0","7","8","9"]';
	exit;
}
else if($a == 'result')	//	开奖结果
{
	$page = max(1,intval($_GET['pageIndex']));
	$size = max(1,intval($_GET['pagesize']));
	$limit = ' LIMIT '.($page * $size - $size).','.$size;
	
	$list = ['total'=>0,'list'=>[]];
	$list['list'] = $db->getall('SELECT period,dateline,rednum AS result FROM '.$table.' WHERE cid = '.$data['id'].' AND rednum != \'\' ORDER BY stoptime DESC '.$limit);
	foreach($list['list'] as $key => $val)
	{
		$list['list'][$key]['date'] = date('Y-m-d',$val['dateline']);
	}
	$list['total'] = count($list['list']);

	echo json_encode($list);
	exit;
}
else if($a == 'result2')	//	开奖结果
{
	$page = max(1,intval($_GET['pageIndex']));
	$size = max(1,intval($_GET['pageSize']));
	$limit = ' LIMIT '.($page * $size - $size).','.$size;
	
	//	开奖时间
	$where = ' FROM '.$table.' WHERE cid = '.$data['id'].' AND rednum != \'\'';
	$dt = NoBadStr($_GET['dtrange']);
	if(strlen($dt) > 4)
	{
		$dt = explode(' - ',$dt);
		$where .= ' AND dateline >= '.strtotime($dt[0]).' AND dateline < '.(strtotime($dt[1]) + 86400);
	}
	
	//	期号
	$period = NoBadStr($_GET['period']);
	$period && $where .= ' AND period LIKE \'%'.$period.'%\' ';
	
	$list = ['total'=>0,'list'=>[]];
	$list['list'] = $db->getall('SELECT id,period,dateline,rednum AS result '.$where.' ORDER BY stoptime DESC '.$limit);
	foreach($list['list'] as $key => $val)
	{
		$list['list'][$key]['year'] = date('Y',$val['dateline']);
		$list['list'][$key]['date'] = date('Y-m-d H:i:s',$val['dateline']);
	}
	
	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);	
	$list['total'] = intval($tmp['p']);

	echo json_encode($list);
	exit;
}
else if($a == 'lost')	//	遗漏
{
	$res = [];
	$lens = ['SSC'=>5,'11X5'=>5,'3D'=>3,'K3'=>3,'KL8'=>5,'PK10'=>10];	//	彩票号码个数
	
	//	上一期
	$lastperiod = $db->getrow('SELECT id FROM '.$table.' WHERE cid = '.$data['id'].' AND rednum != \'\' ORDER BY stoptime DESC');
	if($lastperiod)
	{
		//	计算遗漏
		$tmp = $db->getrow('SELECT * FROM '.$ltable.' WHERE iid = '.$lastperiod['id']);
		for($i = 0; $i < $lens[$data['ptid']]; $i++)
		{
			$res[$i] = [];
			$res[$i]['Local'] = $i;
			$res[$i]['CodeNums'] = [];
			
			$t2 = explode(',',$tmp['data_'.$i]);
			foreach($t2 as $key2 => $val2)
			{
				//	AC：冷热，MC：遗漏
				$res[$i]['CodeNums'][] = ['Code'=>$key2,'AC'=>0,'MC'=>intval($val2)];
			}
		}
	}
	
	echo json_encode($res);
	exit;
}
else if($a == 'lost2')	//	遗漏
{
	$res = [];
	$lens = ['SSC'=>5,'11X5'=>5,'3D'=>3,'K3'=>3,'KL8'=>5,'PK10'=>10];	//	彩票号码个数
	
	$period = intval($_GET['periodID']);
	if($period > 0)
	{
		$tmp = $db->getrow('SELECT * FROM '.$ltable.' WHERE iid = '.$period);
		for($i = 0; $i < $lens[$data['id']]; $i++)
		{
			$res[$i] = [];
			$res[$i]['Local'] = $i;
			$res[$i]['CodeNums'] = [];
			
			$t2 = explode(',',$tmp['data_'.$i]);
			foreach($t2 as $key2 => $val2)
			{
				//	AC：冷热，MC：遗漏
				$res[$i]['CodeNums'][] = ['Code'=>$key2,'AC'=>0,'MC'=>intval($val2)];
			}
		}
	}
	
	echo json_encode($res);
	exit;
}
else if($a == 'rule')
{
	$tmp = $db->getrow('SELECT * FROM '.Table('article').' WHERE cid = 9 AND lotteryid = '.$data['id'].' AND status = 1');
	$rule = $tmp['content'];
	
	echo '<div class="main-tit">',$data['title'],'玩法介绍</div><div class="table-list">',$rule,'</div>';
	exit;
}
else if($a == 'rulecredit')	//	信用玩法规则
{
	$str = $rule = '';
	
	$j = 0;
	foreach($lottery as $val)
	{
		if($val['credit'])
		{
			$str .= '<li'.(!$j ? ' class="curr"' : '').' data-id="'.$val['id'].'"><a href="javascript:;">'.$val['title'].'</a></li>';
		
			$tmp = $db->getrow('SELECT content FROM '.Table('article').' WHERE cid = 9 AND lotteryid = '.$val['id'].' AND status = 1');
			$rule .= '<div class="gz_item" data-id="'.$val['id'].'"'.(!$j ? ' style="display:block"' : '').'>'.$tmp['content'].'</div>';
			
			$j++;
		}
	}
	
	echo '<div class="gz_tab clear" style="padding:0 10px;"><ul rule="nav">',$str,'</ul></div><div class="gz_con" rule="content" style="padding:10px;">',$rule,'</div>';
	exit;
}
else if($a == 'rate')	//	赔率
{
	
	echo '{"status":true,"info":"获取赔率变化成功！","data":"[]"}';
	exit;
}
else if($a == 'longhudou')	//	龙虎斗
{
	echo '[]';
	exit;
}
else if($a == 'forget')	//	遗漏数据
{
	echo '[{"GamePlayID":217,"GameName":"总和单双","ItemName":"总双","Count":1},{"GamePlayID":216,"GameName":"总和大小","ItemName":"总大","Count":1},{"GamePlayID":219,"GameName":"第一球大小","ItemName":"小","Count":2},{"GamePlayID":220,"GameName":"第一球单双","ItemName":"双","Count":3},{"GamePlayID":222,"GameName":"第二球大小","ItemName":"大","Count":2},{"GamePlayID":223,"GameName":"第二球单双","ItemName":"单","Count":4},{"GamePlayID":225,"GameName":"第三球大小","ItemName":"大","Count":1},{"GamePlayID":226,"GameName":"第三球单双","ItemName":"单","Count":2},{"GamePlayID":228,"GameName":"第四球大小","ItemName":"大","Count":3},{"GamePlayID":229,"GameName":"第四球单双","ItemName":"单","Count":3},{"GamePlayID":231,"GameName":"第五球大小","ItemName":"小","Count":3},{"GamePlayID":232,"GameName":"第五球单双","ItemName":"单","Count":1},{"GamePlayID":554,"GameName":"前三","ItemName":"豹子","Count":55},{"GamePlayID":554,"GameName":"前三","ItemName":"顺子","Count":9},{"GamePlayID":554,"GameName":"前三","ItemName":"对子","Count":8},{"GamePlayID":554,"GameName":"前三","ItemName":"半顺","Count":4},{"GamePlayID":555,"GameName":"中三","ItemName":"豹子","Count":100},{"GamePlayID":555,"GameName":"中三","ItemName":"顺子","Count":53},{"GamePlayID":555,"GameName":"中三","ItemName":"杂六","Count":3},{"GamePlayID":555,"GameName":"中三","ItemName":"半顺","Count":2},{"GamePlayID":556,"GameName":"后三","ItemName":"豹子","Count":100},{"GamePlayID":556,"GameName":"后三","ItemName":"顺子","Count":5},{"GamePlayID":556,"GameName":"后三","ItemName":"杂六","Count":1},{"GamePlayID":556,"GameName":"后三","ItemName":"半顺","Count":2}]';
	exit;
}